﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;

namespace PlanWater.Helper
{
    public class PictureHelper
    {
        /// <summary>
        /// 根据URL获取图片流程
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static MemoryStream GetImageStream(string url)
        {
            try
            {
                MemoryStream memoryStream;
                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method = "GET";
                var v = HttpWebRequest.GetSystemWebProxy();
                webRequest.Proxy = v;
                //myRequest.ServicePoint.BindIPEndPointDelegate(

                WebResponse webResponse = webRequest.GetResponse();
                using (var stream = webResponse.GetResponseStream())
                { 
                   using(memoryStream=new MemoryStream())
                   {
                       stream.CopyTo(memoryStream);
                   } 
                }

                return memoryStream;
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("获取图片流错误！URL:{0};错误信息:{1}", url, e.ToString()));
            }
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="saveFilePath"></param>
        /// <param name="fileName"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool SaveImage(MemoryStream stream, string saveFilePath, string fileName, out string filePath)
        {
            bool flag = false;
            try
            { 
                var picBytes = stream.ToArray();
                flag = SaveImage(picBytes, saveFilePath, fileName, out filePath);
            }
            catch (Exception e)
            {
                throw e;
            }
             return flag;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="picBytes">二进制图片</param>
        /// <param name="saveFilePath"></param>
        /// <param name="fileName"></param>
        /// <param name="filePath">完整路径</param>
        /// <returns></returns>
        public static bool SaveImage(byte[] picBytes, string saveFilePath, string fileName,out string filePath)
        {
            try
            {
                var top2String = picBytes[0].ToString() + picBytes[1].ToString();
                string path = Path.Combine(saveFilePath, fileName + CheckImageStreamType(top2String));
                filePath = path;
                var value = CheckImageStreamType(top2String);
                using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    fileStream.Write(picBytes, 0, picBytes.Length);
                    fileStream.Flush();
                    fileStream.Close();
                }
                return File.Exists(path);
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("保存图片流错误！错误信息：{0}", e.ToString()));
            }
        } 

        /// <summary>
        /// 验证图片流程类型
        /// </summary>
        /// <param name="topTwoByteString"></param>
        /// <returns></returns>
        private static string CheckImageStreamType(string topTwoByteString)
        {
            string type = string.Empty;
            type = string.Format(".{0}",Enum.Parse(typeof(PicFileExtension), topTwoByteString).ToString());
            return type;
        }

        /// <summary>
        /// 图片字符串转成二进制
        /// </summary>
        /// <param name="picString"></param>
        /// <returns></returns>
        public static byte[] PicStringToyBytes(string picString)
        {
            byte[] bytes =Convert.FromBase64String(picString);
            return bytes;
        }

        /// <summary>
        /// 图片转成二进制
        /// </summary>
        /// <param name="picPath"></param>
        /// <returns></returns>
        public static byte[] GetPictureData(string picPath)
        {
            //根据图片文件的路径使用文件流打开，并保存为byte[]   
            FileStream fs = new FileStream(picPath, FileMode.Open);//可以是其他重载方法   
            byte[] byData = new byte[fs.Length];
            fs.Read(byData, 0, byData.Length);
            fs.Close();
            return byData;  
        }

        /// <summary>
        /// 图片流程图片类型枚举
        /// </summary>
        private enum PicFileExtension {
            jpg = 255216,
            gif = 7173,
            png = 13780,
        }


    }
}
